<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="userInfo" label-width="100px" :model="userInfo" :rules="rules">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input v-model="userInfo.departmentName" @focus="tree=true" />
            <el-tree
              v-show="tree"
              :data="Departments"
              :props="{label:'name'}"
              @node-click="handleNodeClick"
            ></el-tree>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.correctionTime"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <Uploadimg :staff-photo.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="submit">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
import { mapActions } from 'vuex'
// import { getDepartments } from '@/api/departments'
// import { transformTreeData } from '@/utils/index'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      tree: false,
      Departments: [],
      rules: {
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  // mounted () {
  //   this.getDepartments()
  // },
  methods: {
    ...mapActions('user', ['getUserInfoAction']),
    async submit () {
      console.log(this.userInfo)
      try {
        await this.$refs.userInfo.validate()
        await saveUserDetailById(this.userInfo)
        this.$message.success('更新成功')
        this.getUserInfoAction()
      } catch (error) {
        console.log(error)
      }
    },
    // async getDepartments () {
    //   const data = await getDepartments()
    //   // console.log(data.data.depts)
    //   this.Departments = transformTreeData(data.data.depts)
    // }
    handleNodeClick (val) {
      //   this.userInfo.correctionTime = val.name
      console.log(val)
    }
  }

}
</script>
